import sys
sys.path.append("plugins/channel")
import threading
import time

class virtualdevice(threading.Thread):
	def __init__(self, parameters):
		threading.Thread.__init__(self)

		if not parameters.has_key("llnplugin"):
			return False

		self.channel = parameters["llnplugin"]
		
		if parameters:
			self.parameters = parameters
		else:
			pass
			# Exception
		
		if self.parameters.has_key("defplugins") and self.parameters["defplugins"] != "none":
			defindex = 0
			defplugins = {} 
			for defplugin in self.parameters["defplugins"].rsplit(','):
				newdefplugin_name = defplugin.replace(' ', '')
				if newdefplugin_name == "none":
					continue

				try:
					exec "from %s import %s" % (newdefplugin_name, newdefplugin_name)
					exec "newdefplugin = %s(self.parameters)" % (newdefplugin_name)
					newdefplugin.start()
				except Exception, e:
					print "Excepcion defplugin", newdefplugin_name, e
					continue	

				defplugins[defindex] = newdefplugin
				defindex += 1
				print "Deformation:", newdefplugin, "active"				

		# INTERFACE PROPERTIES
		self.properties = {}
		self.properties["status"] = False
		self.properties["MTU"] = 1500
		self.properties["RXpackets"] = 0
		self.properties["RXerrors"] = 0
		self.properties["RXdropped"] = 0
		self.properties["RXqueued"] = 0
		self.properties["TXpackets"] = 0
		self.properties["TXerrors"] = 0
		self.properties["TXdropped"] = 0
		self.properties["TXqueued"] = 0
		
		self.RXPreProcessQueue = []
		self.TXPreProcessQueue = []

		self.RXPostProcessQueue = []
		self.TXPostProcessQueue = []

		self.llnplugin = False

		print self.parameters

	def run(self):	
		while 1:
			if self.properties["status"]:				
				if self.RXPreProcessQueue.__len__() > 0:
					# Process packet and send to RXPostProcessQueue
					pass

				if self.TXPreProcessQueue.__len__() > 0:
					# Process packet and send to TXPostProcessQueue
					pass

				# Update queue counters
				self.properties["RXqueued"] = self.RXPostProcessQueue.__len__() + self.RXPreProcessQueue.__len__()
				self.properties["TXqueued"] = self.TXPostProcessQueue.__len__() + self.TXPreProcessQueue.__len__()

				# Get and put packet in llnplugin queues
				if self.llnplugin:
					if self.llnplugin.RXQueue.__len__() > 0:
						self.RXPreProcessQueue.append(self.llnplugin.RXQueue.pop())

				if self.TXPostProcessQueue.__len__() > 0:
					self.llnplugin.TXQueue.append(self.TXPostProcessQueue.pop())

			else:
				time.sleep(0.01)
			
			time.sleep(0.01)

	def sendpacket(self, packet):
		if self.properties["status"]:
			# Check packet ?
			self.TXPreProcessQueue.append(packet)
			return True
		else:
			return False

	def recvpacket(self):
		if self.RXPostProcessQueue.__len__() > 0:
			return self.RXPostProcessQueue.pop()
		else:
			return False

	def up(self):
		if not self.llnplugin:
			try:
				try:
					exec "from %s import %s" % (self.channel, self.channel)
					exec "self.llnplugin = %s(self.parameters)" % (self.channel)
					self.llnplugin.start()
					self.properties["status"] = True
				except Exception, e:
					print "Excepcion tunnelchannel", e
					return False

				if type(self.llnplugin) != type(True):
					return True
			except:
				return False
		else:
			try:
				self.llnplugin.up()
				if not self.llnplugin.status:
					return False
			except:
				return False

		return True

	def down(self):
		try:
			self.llnplugin.down()
			self.properties["status"] = False
			return True
		except:
			return False
		return False

	def _processrecvpacket(self, packet):
		# Processing recv packet stuff
		pass

	def _processsendpacket(self, packet):
		# Processing send packet stuff
		pass
